package com.bnlr.lrparking.appui.web;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.util.APPUtil;

/**
 * 点击banner 跳转到本页面加载 web view
 *
 * @author hp
 * @date 2017/10/12
 */
public class BannerWebActivity extends BaseActivity {

    private WebView web_invite;
    private String Url;
    private TextView titleView;
    private RelativeLayout rlBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        titleView = (TextView) findViewById(R.id.tv_nav_title);
        rlBack = (RelativeLayout) findViewById(R.id.rela_nav_back);
        initView();
    }

    private void initView() {

        Intent intent = getIntent();
        Url = intent.getStringExtra("Url");
        web_invite = (WebView) findViewById(R.id.web_invite);
        WebSettings webSettings = web_invite.getSettings();
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        web_invite.loadUrl(Url);
        web_invite.getSettings().setJavaScriptEnabled(true);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        web_invite.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                Log.e("用户单击超连接", url + "");
                //判断用户单击的是那个超连接
                String tag = "tel";
                if (url.contains(tag)) {
                    String mobile = url.substring(url.lastIndexOf(":") + 1);
                    Log.e("mobile----------->", mobile);
                    Intent mIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ResolveInfo resolveInfo = getPackageManager().resolveActivity(mIntent, 0);
                    if (resolveInfo!=null){
                        LogUtils.i("ACTION_DIAL"+resolveInfo.activityInfo.packageName);
                        //Android6.0以后的动态获取打电话权限
                        if (ActivityCompat.checkSelfPermission(BannerWebActivity.this, android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                            startActivity(mIntent);
                            //这个超连接,java已经处理了，webview不要处理
                            return true;
                        } else {
                            //申请权限
                            ActivityCompat.requestPermissions(BannerWebActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                            return true;
                        }
                    }

                } else { // 点击不包含电话信息的超链接
                  web_invite.loadUrl(url);
                }
                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //解决页面中有代码控制回退导致回退紊乱的问题，返回false代表将操作交给webView来处理回退问题
                return false;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (errorCode == (WebViewClient.ERROR_CONNECT
                        | WebViewClient.ERROR_IO
                        | WebViewClient.ERROR_TIMEOUT
                        | WebViewClient.ERROR_BAD_URL)) {
                    titleView.setText("加载失败");
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String title = view.getTitle();
                if (title != null) {
                    if (TextUtils.equals("找不到网页", title)) {
                        title = "加载失败";
                    }
                } else {
                    title = "";
                }
                titleView.setText(title);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                titleView.setText("加载中...");
            }
        });
    }


    @Override
    protected void onResume() {
        if (web_invite != null) {
            web_invite.onResume();
            web_invite.resumeTimers();
        }
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (web_invite != null) {
            web_invite.reload();
            web_invite.onPause();
            web_invite.clearFormData();
            web_invite.pauseTimers();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (web_invite != null) {
            web_invite.clearFormData();
            web_invite.destroy();
            web_invite = null;
        }
        super.onDestroy();
    }
}
